package com.defense;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RanGroup {
    JFrame jf = new JFrame("随机小组");
    final int width = 500;
    final int height = 600;

    // 用于存储groups.txt中的内容
    private List<String> groupList = new ArrayList<>();
    private boolean isRandomRunning = false;
    private JTextField groupTextField;
    // 随机按钮（针对小组）
    private JButton groupRandomBtn;
    // 最下面的随机按钮
    private JButton randomBtn;
    // 用于存储name.txt中的内容
    private List<String> nameList = new ArrayList<>();
    private JTextField nameTextField;

    // 组装视图
    public void init() {
        jf.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - width) / 2,
                (Toolkit.getDefaultToolkit().getScreenSize().height - height) / 2,
                width, height);
        jf.setResizable(false);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 设置JFrame的布局为BorderLayout
        jf.setLayout(new BorderLayout());

        // 创建标题标签，并设置其水平居中
        JLabel titleLabel = new JLabel("随机小组", SwingConstants.CENTER);
        jf.add(titleLabel, BorderLayout.NORTH);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
        centerPanel.setAlignmentX(Component.CENTER_ALIGNMENT);

        // 创建小组输入框和随机按钮
        groupTextField = new JTextField(8); // 调小可输入字符宽度为8
        JLabel groupLabel = new JLabel("小组:");
        groupRandomBtn = new JButton("随机");
        Box groupBox = Box.createHorizontalBox();
        groupBox.add(groupLabel);
        groupBox.add(Box.createHorizontalStrut(5));
        groupBox.add(groupTextField);
        groupBox.add(Box.createHorizontalStrut(10));
        groupBox.add(groupRandomBtn);
        centerPanel.add(groupBox);

        // 创建姓名输入框
         nameTextField = new JTextField(8); // 调小可入字符宽度为8
        JLabel nameLabel = new JLabel("姓名:");
        Box nameBox = Box.createHorizontalBox();
        nameBox.add(nameLabel);
        nameBox.add(Box.createHorizontalStrut(5));
        nameBox.add(nameTextField);
        centerPanel.add(Box.createVerticalStrut(10));
        centerPanel.add(nameBox);

        // 创建文字标签
        JLabel textLabel = new JLabel("图片:", SwingConstants.CENTER);
        centerPanel.add(textLabel);

        // 图片
        URL imageUrl = getClass().getResource("屏幕截图 2024-11-26 110005.png");
        ImageIcon imageIcon = new ImageIcon(imageUrl);
        // 图片进行缩放
        Image scaledImage = imageIcon.getImage().getScaledInstance(width - 200, -150, Image.SCALE_SMOOTH);
        JLabel imageLabel = new JLabel(new ImageIcon(scaledImage));
        imageLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        centerPanel.add(imageLabel);

        // 创建分数输入框
        JTextField scoreTextField = new JTextField(5);
        JLabel scoreLabel = new JLabel("请输入分数:");
        Box scoreBox = Box.createHorizontalBox();
        scoreBox.add(scoreLabel);
        scoreBox.add(Box.createHorizontalStrut(5));
        scoreBox.add(scoreTextField);
        centerPanel.add(Box.createVerticalStrut(10));
        centerPanel.add(scoreBox);

        // 创建按钮
        randomBtn = new JButton("随机");
        JButton rateBtn1 = new JButton("评分");
        JButton rateBtn2 = new JButton("评分");
        JButton rateBtn3 = new JButton("评分");
        JButton backBtn = new JButton("返回");

        // 为返回按钮添加事件监听器
        backBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MangerInterface mangerInterface = new MangerInterface();
                mangerInterface.init();
                jf.setVisible(false);
                jf.dispose();
            }
        });

        // 读取groups.txt和name.txt文件内容
        readGroupFile();
        readNameFile();

        // 为小组随机按钮添加事件监听器
        groupRandomBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (groupRandomBtn.getText().equals("随机")) {
                    startRandomSelectionForGroup();
                    groupRandomBtn.setText("停");
                } else {
                    stopRandomSelectionForGroup();
                    groupRandomBtn.setText("随机");
                    // 将当前随机到的结果放入小组输入框
                    int randomIndex = new Random().nextInt(groupList.size());
                    groupTextField.setText(groupList.get(randomIndex));
                }
            }
        });

        // 为最下面的随机按钮添加事件监听器
        randomBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (randomBtn.getText().equals("随机")) {
                    startRandomSelectionForName();
                    randomBtn.setText("停");
                } else {
                    stopRandomSelectionForName();
                    randomBtn.setText("随机");
                    // 将当前随机到的结果放入姓名输入框
                    int randomIndex = new Random().nextInt(nameList.size());
                    nameTextField.setText(nameList.get(randomIndex));
                }
            }
        });

        // 为评分按钮添加事件监听器
        rateBtn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showRatingSuccessPopup();
            }
        });
        rateBtn2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showRatingSuccessPopup();
            }
        });
        rateBtn3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showRatingSuccessPopup();
            }
        });

        Box buttonBox = Box.createHorizontalBox();
        buttonBox.add(randomBtn);
        buttonBox.add(Box.createHorizontalStrut(15)); // 适当减小水平间距
        buttonBox.add(rateBtn1);
        buttonBox.add(Box.createHorizontalStrut(15));
        buttonBox.add(rateBtn2);
        buttonBox.add(Box.createHorizontalStrut(15));
        buttonBox.add(rateBtn3);
        buttonBox.add(Box.createHorizontalStrut(15));
        buttonBox.add(backBtn);

        centerPanel.add(Box.createVerticalStrut(15)); // 添加一些垂直间距
        centerPanel.add(buttonBox);

        jf.add(centerPanel, BorderLayout.CENTER);

        // 显示窗口
        jf.setVisible(true);
    }

    // 读取groups.txt文件内容到groupList
    private void readGroupFile() {
        File groupFile = new File("groups.txt");
        if (groupFile.exists()) {
            try (BufferedReader br = new BufferedReader(new FileReader(groupFile))) {
                String line;
                while ((line = br.readLine())!= null) {
                    groupList.add(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 读取name.txt文件内容到nameList
    private void readNameFile() {
        File nameFile = new File("name.txt");
        if (nameFile.exists()) {
            try (BufferedReader br = new BufferedReader(new FileReader(nameFile))) {
                String line;
                while ((line = br.readLine())!= null) {
                    nameList.add(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 小组的随机选择数据
    private void startRandomSelectionForGroup() {
        isRandomRunning = true;
        new Thread(() -> {
            while (isRandomRunning) {
                int randomIndex = new Random().nextInt(groupList.size());
                groupTextField.setText(groupList.get(randomIndex));
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
    }

    // 停止小组的随机选择数据
    private void stopRandomSelectionForGroup() {
        isRandomRunning = false;
    }

    // 开始name.txt中数据的随机选择
    private void startRandomSelectionForName() {
        isRandomRunning = true;
        new Thread(() -> {
            while (isRandomRunning) {
                int randomIndex = new Random().nextInt(nameList.size());
                nameTextField.setText(nameList.get(randomIndex));
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
    }

    // 停止name.txt中数据的随机选择
    private void stopRandomSelectionForName() {
        isRandomRunning = false;
    }
    private void showRatingSuccessPopup() {
        JOptionPane.showMessageDialog(jf, "评分成功");
    }

    public static void main(String[] args) {
        new RanGroup().init();
    }
}